From 8418e6422f0ec67c51d472321f4704a3a16e0368 Mon Sep 17 00:00:00 2001 From: Marek Kasik Date: Thu, 19 Dec 2013 14:52:16 +0100 Subject: [PATCH] printing: Enable search through locations in printers list Add printer_compare() function for comparing printers according to their names and locations. It is possible to search by multiple keys separated by space or tabulator using logical conjunction. Based on patch by William Hua. https://bugzilla.gnome.org/show_bug.cgi?id=692931 --- gtk/gtkprintunixdialog.c | 87 ++++++++++++++++++++++++++ gtk/resources/ui/gtkprintunixdialog.ui | 1 - 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/gtk/gtkprintunixdialog.c b/gtk/gtkprintunixdialog.c index b83b10034a..0d2569bc03 100644 --- a/gtk/gtkprintunixdialog.c +++ b/gtk/gtkprintunixdialog.c @@ -212,6 +212,11 @@ static gboolean set_active_printer (GtkPrintUnixDialog *dialog, const gchar *printer_name); static void redraw_page_layout_preview (GtkPrintUnixDialog *dialog); static void load_print_backends (GtkPrintUnixDialog *dialog); +static gboolean printer_compare (GtkTreeModel *model, + gint column, + const gchar *key, + GtkTreeIter *iter, + gpointer search_data); /* GtkBuildable */ static void gtk_print_unix_dialog_buildable_init (GtkBuildableIface *iface); @@ -758,6 +763,9 @@ gtk_print_unix_dialog_init (GtkPrintUnixDialog *dialog) GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING); + gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (priv->printer_treeview), + printer_compare, NULL, NULL); + gtk_tree_view_column_set_cell_data_func (priv->printer_icon_column, priv->printer_icon_renderer, set_cell_sensitivity_func, NULL, NULL); @@ -2106,6 +2114,85 @@ selected_printer_changed (GtkTreeSelection *selection, g_object_notify ( G_OBJECT(dialog), "selected-printer"); } +static gboolean +printer_compare (GtkTreeModel *model, + gint column, + const gchar *key, + GtkTreeIter *iter, + gpointer search_data) +{ + gboolean matches = FALSE; + + if (key != NULL) + { + gchar *name = NULL; + gchar *location = NULL; + gchar *casefold_key = NULL; + gchar *casefold_name = NULL; + gchar *casefold_location = NULL; + gchar **keys; + gchar *tmp1, *tmp2; + gint i; + + gtk_tree_model_get (model, iter, + PRINTER_LIST_COL_NAME, &name, + PRINTER_LIST_COL_LOCATION, &location, + -1); + + casefold_key = g_utf8_casefold (key, -1); + + if (name != NULL) + { + casefold_name = g_utf8_casefold (name, -1); + g_free (name); + } + + if (location != NULL) + { + casefold_location = g_utf8_casefold (location, -1); + g_free (location); + } + + if (casefold_name != NULL || + casefold_location != NULL) + { + keys = g_strsplit_set (casefold_key, " \t", 0); + if (keys != NULL) + { + matches = TRUE; + + for (i = 0; keys[i] != NULL; i++) + { + if (keys[i][0] != '\0') + { + tmp1 = tmp2 = NULL; + + if (casefold_name != NULL) + tmp1 = g_strstr_len (casefold_name, -1, keys[i]); + + if (casefold_location != NULL) + tmp2 = g_strstr_len (casefold_location, -1, keys[i]); + + if (tmp1 == NULL && tmp2 == NULL) + { + matches = FALSE; + break; + } + } + } + + g_strfreev (keys); + } + } + + g_free (casefold_location); + g_free (casefold_name); + g_free (casefold_key); + } + + return !matches; +} + static void update_collate_icon (GtkToggleButton *toggle_button, GtkPrintUnixDialog *dialog) diff --git a/gtk/resources/ui/gtkprintunixdialog.ui b/gtk/resources/ui/gtkprintunixdialog.ui index 3d2aec7d39..cf8cbf74c7 100644 --- a/gtk/resources/ui/gtkprintunixdialog.ui +++ b/gtk/resources/ui/gtkprintunixdialog.ui @@ -93,7 +93,6 @@ True True printer_list_filter - 1 -- 2.30.2